package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckGroupException;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version V1.0
 * @author: ljx
 * @date: 2020/01/11 20:20
 * @description:
 */
@RequestMapping("/checkGroup")
@RestController
public class CheckGroupController {

    @Reference(cluster = "failfast")
    private CheckGroupService checkGroupService;

    @RequestMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup) {
        Result result = null;
        try {
            checkGroupService.add(checkGroup);
            result = new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return result;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findGroupMsg(@PathVariable("id") Integer id) {
        Result result = null;
        try {
            result = checkGroupService.findGroupMsg(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        PageResult pageResult = null;
        try {
            pageResult = checkGroupService.findPage(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult (new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL));
        }
        return pageResult;
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup){
        Result result = null;
        try {
            result=checkGroupService.edit(checkGroup);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return result;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") Integer id ){
        Result result = null;
        try {
            result = checkGroupService.delete(id);
        }catch (CheckGroupException e){
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return result;
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        Result result = null;
        try {
            result = checkGroupService.findAll();
        } catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        return result;
    }
}
